package com.sys.server.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sys.server.common.config.CommonConfig;
import com.sys.server.common.constants.enums.ResultSetEnum;
import com.sys.server.common.model.R;
import com.sys.server.modules.controller.dto.StandardDto;
import com.sys.server.modules.controller.vo.PageVo;
import com.sys.server.modules.entity.NormasStandardEntity;
import com.sys.server.modules.mapper.NormasStandardMapper;
import com.sys.server.modules.service.FileService;
import com.sys.server.modules.service.NormasStandardService;
import com.sys.server.modules.service.TokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class NormasStandardServiceImpl implements NormasStandardService {

    private static final Logger LOG = LoggerFactory.getLogger(NormasStandardServiceImpl.class);

    @Autowired
    private TokenService tokenService;

    @Autowired
    private FileService fileService;

    @Resource
    private NormasStandardMapper standardMapper;

    @Override
    public R createStandard(StandardDto standardDto) {
        String num = standardDto.getNumber();
        NormasStandardEntity standardEntity = null;
        standardEntity = standardMapper.selectOne(Wrappers.<NormasStandardEntity>query().eq("number", num));
        if (standardEntity == null) {
            standardEntity = new NormasStandardEntity();
        }
        // 标准号
        standardEntity.setNumber(standardDto.getNumber());
        // 标准名
        standardEntity.setSname(standardDto.getSname());
        // 标准类型
        standardEntity.setType(standardDto.getType());
        // 起草单位
        standardEntity.setDrdepart(standardDto.getDrdepart());
        // 发布单位
        standardEntity.setReldepart(standardDto.getReldepart());
        // 发布日期
        standardEntity.setReldate(standardDto.getReldate());
        // 实施日期
        standardEntity.setEndate(standardDto.getEndate());
        // 正文内容
        standardEntity.setMaintext(standardDto.getMaintext());
        // 发布用户
        LOG.info(tokenService.getCurrentUser());
        standardEntity.setReluser(tokenService.getCurrentUser());
        /** 文件上传 */
        String paths = CommonConfig.getAppendix_path() + "/standard/" + standardDto.getNumber();
        String path = fileService.fileUpload(standardDto.getFile(), paths);
        if (StringUtils.isNotBlank(path)) {
            standardEntity.setAttachment(path);
        } else {
            LOG.error("获取行业标准附件路径错误");
            return R.ok(ResultSetEnum.FAIL);
        }
        try {
            int row = standardMapper.insert(standardEntity);
            if (row == 1) {
                return R.ok(ResultSetEnum.SUCCESS);
            }
        } catch (Exception e) {
            LOG.error("新增行业标准错误" + e.getMessage());
        }
        return R.ok(ResultSetEnum.FAIL);
    }

    /**
     * 行业标准列表分页查询
     *
     * @return
     */
    @Override
    public R getStandardList(Long current, Long size) {
        IPage<NormasStandardEntity> page = new Page<>(current, size);
        try {
            standardMapper.selectPage(page, null);
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error("行业标准分页查询错误");
            return R.ok(ResultSetEnum.FAIL);
        }
        PageVo result = new PageVo();
        if (page.getRecords() != null) {
            result.setCurrent(current);
            result.setList(page.getRecords());
            result.setSize(size);
            result.setTotal(page.getTotal());
        }
        return R.ok(ResultSetEnum.SUCCESS, result);
    }

    /**
     * 返回标准正文内容
     *
     * @param number
     * @return
     */
    @Override
    public R getDetailInfo(String number) {
        NormasStandardEntity standardEntity = standardMapper.selectOne(Wrappers.<NormasStandardEntity>query().eq("number", number));
        if (standardEntity != null) {
            return R.ok(ResultSetEnum.SUCCESS, standardEntity);
        }
        return R.ok(ResultSetEnum.FAIL);
    }

    @Override
    public String downloadStandardAttach(String number) throws IOException {
        NormasStandardEntity entity = standardMapper.selectOne(Wrappers.<NormasStandardEntity>query().eq("number", number));
        String path = entity.getAttachment();
        return path;
    }

    @Override
    public R updateStandardInfo(StandardDto standardDto) {
        String number = standardDto.getNumber();
        NormasStandardEntity entity = standardMapper.selectOne(Wrappers.<NormasStandardEntity>query().eq("number", number));
        entity.setSname(standardDto.getSname());
        entity.setMaintext(standardDto.getMaintext());
        entity.setType(standardDto.getType());
        entity.setDrdepart(standardDto.getDrdepart());
        entity.setReldepart(standardDto.getReldepart());
        entity.setEndate(standardDto.getEndate());
        entity.setReldate(standardDto.getReldate());
        UpdateWrapper<NormasStandardEntity> wrapper = new UpdateWrapper<>();
        wrapper.eq("number", number);
        try {
            standardMapper.update(entity, wrapper);
        } catch (Exception e) {
            LOG.error("更新行业标准信息（无附件）错误" + e.getMessage());
            return R.ok(ResultSetEnum.FAIL);
        }
        return R.ok(ResultSetEnum.SUCCESS);
    }

    @Override
    public R deleteStandard(StandardDto standardDto) {
        String number = standardDto.getNumber();
        try {
            standardMapper.delete(Wrappers.<NormasStandardEntity>query().eq("number", number));
        } catch (Exception e) {
            LOG.error("删除行业标准错误" + e.getMessage());
            return R.ok(ResultSetEnum.FAIL);
        }
        return R.ok(ResultSetEnum.SUCCESS);
    }

}
